import tkinter as tk
from tkinter import filedialog
from controllers.word_table_line2solid_controller import WordTableLine2solidController

import config


class WordTableLine2SolidWindow(tk.Frame):
    def __init__(self, master, main_window):
        super().__init__(master)
        self.entry_frame = None
        self.page_label = None
        self.status_label = None
        self.submit_button = None
        self.file_button = None
        self.entry = None
        self.main_window = main_window
        self.create_widgets()

    def create_widgets(self):
        self.entry_frame = tk.Frame(self)

        # 页面标签
        self.page_label = tk.Label(self, text="word文档表格线条转实线")
        self.page_label.pack(anchor="w", padx=10, pady=10)

        self.entry = tk.Entry(self.entry_frame, width=config.ENTRY_WIDTH)
        self.entry.grid(row=0, column=0)

        self.file_button = tk.Button(self.entry_frame, text="浏览", command=self.browse_file)
        self.file_button.grid(row=0, column=1)

        # 将输入框和文件选择按钮放在同一个框架中
        self.entry_frame.pack(fill='x', padx=10, pady=10)
        
        # 提交按钮
        self.submit_button = tk.Button(self, text="开始", command=self.submit)
        self.submit_button.pack(anchor="w", padx=10, pady=10, ipadx=50)

        # 状态标签
        self.status_label = tk.Label(self, text="")
        self.status_label.pack(pady=5)


        

    def browse_file(self):
        # 打开文件选择对话框
        filename = filedialog.askopenfilename()
        self.entry.delete(0, tk.END)
        self.entry.insert(0, filename)

    def submit(self):
        # 获取输入框中的值并打印
        input_file_path = self.entry.get()
        print("您输入的地址或文件路径为:", input_file_path)
        self.status_label.config(text="生成中...")
        self.submit_button.config(state=tk.DISABLED)
        self.master.update_idletasks()

        try:
            controller = WordTableLine2solidController(self.master)
            output_word_path = controller.table2default(input_file_path)
            print(f"文档已保存至: {output_word_path}")
            self.status_label.config(text=f"已成功生成：{output_word_path}")
        except Exception as e:
            print(e)
            self.status_label.config(text=f"生成失败：{e}")
        self.submit_button.config(state=tk.NORMAL)